package com.huasheng.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.huasheng.controller.admin.AbstractController;
import com.huasheng.entity.StoryEntity;
import com.huasheng.entity.SysMenuEntity;
import com.huasheng.entity.SysUserEntity;
import com.huasheng.service.StoryService;
import com.huasheng.service.SysUserLoginLogService;
import com.huasheng.service.SysUserRoleService;
import com.huasheng.service.SysUserService;
import com.huasheng.util.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 门店管理
 *
 * @author Joey
 * @Email wangguoqing_it@163.com
 */
@Controller
@RequestMapping("/admin/sys/story")
public class StoryController extends AbstractController {

    @Resource
    private StoryService storyService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:story:list")
    @ResponseBody
    public R list(Integer offset, Integer limit, String sort, String order,
                  @RequestParam(name = "search", required = false) String storyName) {
        String userName = null;
        String email = null;
        Map<String, String> searchList = parseObject(storyName, "q_userName", "q_email");
        if (null != searchList) {
            userName = searchList.get("q_userName");
            email = searchList.get("q_email");
        }
        offset = (offset / limit) + 1;
        Boolean flag = null; // 排序逻辑
        if (StringUtils.isNoneBlank(order)) {
            if (order.equalsIgnoreCase("asc")) {
                flag = true;
            } else {
                flag = false;
            }
        }
        Page<StoryEntity> adminList = storyService.queryListByPage(offset, limit, email, userName, sort, flag);
        PageUtils pageUtil = new PageUtils(adminList.getRecords(), adminList.getTotal(), adminList.getSize(),
                adminList.getCurrent());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:story:save")
    public R save(StoryEntity story) {
        // 数据校验
        verifyForm(story);
        if (storyService.insert(story)) {
            return R.ok();
        } else {
            return R.error("保存失败!");
        }
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(StoryEntity story) {
        if (StringUtils.isBlank(story.getStoryName())) {
            throw new RRException("门店名称不能为空");
        }

        if (story.getStoryOwner() == null) {
            throw new RRException("店长不能为空");
        }
        story.setRegisterTime(DateUtils.getCurrentUnixTime());
        story.setCreateTime(DateUtils.getCurrentUnixTime());
        story.setUpdateTime(DateUtils.getCurrentUnixTime());
    }
}
